package com.ace.user.common.bean;

import com.ace.user.common.constant.C;
import lombok.*;
import lombok.experimental.Accessors;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * 公共返回参数实体类
 *
 * @Author yudong
 * @Date 2018年07月02日 下午5:21
 */
@Data
@Accessors(chain = true)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class ResponseDTO<T> implements Serializable {

    private static final long serialVersionUID = -2869850010092953027L;

    public ResponseDTO() {
    }

    public ResponseDTO(@NotNull(message = "{requestDTO.message}") String message, @NotNull(message = "{requestDTO.code}") int code) {
        this("",   "", message, code, null);
    }

    /**
     * 版本号
     */
    @NotNull(message = "{requestDTO.version}")
    private String version;
    /**
     * 类型根据type的类型来确定访问者的类型
     */
    @NotNull(message = "{requestDTO.type}")
    private String type;

    /**
     * 消息开发者
     */
    @NotNull(message = "{requestDTO.message}")
    private String message;
    /**
     * 状态码
     */
    @NotNull(message = "{requestDTO.code}")
    private int code;
    /**
     * 接口数据
     */
    private T data;

    /**
     * 复制对象
     *
     * @return
     */
    public ResponseDTO copy() {
        ResponseDTO responseDTO = new ResponseDTO();
        BeanUtils.copyProperties(this, responseDTO);
        return responseDTO;
    }

    /**
     * 判断请求是否成功
     *
     * @return
     */
    public boolean ok() {
        return this.getCode() == C.N_0 ? true : false;
    }
}
